"""
Case Type   : 内核告警
Case Name   : dn实例出现假死异常，测试告警日志信息
Create At   : 2025-10-10
Owner       : lvlintao
Description :
    1、dn实例出现假死异常
    2、测试告警日志信息
    3、清理环境
Expect      :
    1、dn实例出现假死异常
    2、告警日志完整清晰
    3、清理环境成功
History     :
"""

import os
import unittest

from yat.test import Node
from yat.test import macro
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

logger = Logger()


class AlarmReportTestCase(unittest.TestCase):
    def setUp(self):
        logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.comsh = CommonSH('PrimaryDbUser')
        self.comm = Common('PrimaryDbUser')
        self.node = Node('PrimaryDbUser')
        self.std_node = Node('Standby2DbUser')
        status = self.comsh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)

    def test_alarm_report(self):
        text = '--step1:数据库假死状态;expect:成功--'
        logger.info(text)
        cmd = f"source {macro.DB_ENV_PATH};" \
              f"cd {macro.DB_INSTANCE_PATH}; mv postgresql.conf postgresql.conf.bak"
        logger.info(cmd)
        res = self.node.sh(cmd).result()
        logger.info(res)
        pid = self.comm.get_pid(self.node, f'{macro.DB_INSTANCE_PATH}')
        logger.info(pid)
        cmd = f"kill -9 {pid}"
        res = self.node.sh(cmd).result()
        logger.info(res)
        time.sleep(5)
        status = self.comsh.get_db_cluster_status(param='detail')
        logger.info(status)
        self.assertTrue("P Down" in status)

        text = '--step2:查看告警日志;expect:成功--'
        cmd = f"cd {macro.PG_LOG_PATH}/../cm/cm_server;ls -l | grep 'system_alarm' | awk '{{{{print $NF}}}}'"
        logger.info(cmd)
        file = self.node.sh(cmd).result()
        logger.info(file)
        cmd = f"cd {macro.PG_LOG_PATH}/../cm/cm_server;tail -n 5 {file}"
        res = self.node.sh(cmd).result()
        logger.info(res)
        flag = ''
        self.assertIn(flag, res, '执行失败' + text)

    def tearDown(self):
        text = '--step3:清理环境，恢复默认值;expect:成功--'
        logger.info(text)
        cmd = f"source {macro.DB_ENV_PATH};" \
              f"cd {macro.DB_INSTANCE_PATH}; mv postgresql.conf.bak postgresql.conf" \
              f"cm_ctl stop;cm_ctl start"
        res = self.node.sh(cmd).result()
        logger.info(res)
        cmd = f"source {macro.DB_ENV_PATH};gs_ctl query |grep local_role"
        res = self.node.sh(cmd).result()
        logger.info(res)
        if 'Primary' not in res:
            cmd = f"source {macro.DB_ENV_PATH};gs_ctl switchover;"
            res = self.node.sh(cmd).result()
            logger.info(res)
            cmd = f"source {macro.DB_ENV_PATH};gs_ctl query |grep local_role"
            res = self.node.sh(cmd).result()
            logger.info(res)
        self.assertIn('Primary', res, '执行失败' + text)
        logger.info(f'-----{os.path.basename(__file__)} end-----')
